import 'package:flutter/material.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x00_aleret_item.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x01_alert_dialog_routes.dart';

class CheckingDialogPage extends  StatelessWidget {


  @override
  Widget build(BuildContext context) {

    bool _withTree = false;
    bool _withTree2 = false;
    bool _withTree3 = false;
    bool _withTree4 = false;

    Future<bool> showDeleteConfirmDialog() {
      
      return showDialog<bool>(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("Tips"),
            content: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text("Are you sure delete current file?"),
                Row(
                  children: <Widget>[
                    Text("Also the sub folders?"),
                    _DialogCheckBox(
                      value: _withTree,
                      onChanged: (value) => _withTree = !_withTree,
                    )
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text("Also the sub folders?"),
                    StatefulBuilder(
                      builder: (context, _setState) {
                        return Checkbox(
                          value: _withTree2,
                          onChanged: (bool value) {
                            /// _setState方法实际就是该StatefulWidget的setState方法，
                            /// 调用后builder方法会重新被调用
                            _setState(() {
                              _withTree2 = !_withTree2;
                            });
                          },
                        );
                      },
                    )
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text("Also the sub folders?"),
                    Checkbox(
                      value: _withTree3,
                      onChanged: (bool value) {
                        (context as Element).markNeedsBuild();
                        _withTree3 = !_withTree3;
                      },
                    )
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text("Also the sub folders ?"),
                    Builder(
                      builder: (ctx) {
                        return Checkbox(
                          value: _withTree4,
                          onChanged: (value) {
                            (ctx as Element).markNeedsBuild();
                            _withTree4 = !_withTree4;
                          },
                        );
                      },
                    )
                  ],
                )
              ],
            ),
            actions: <Widget>[
              FlatButton(
                child: Text("Cancel"),
                onPressed: () => Navigator.of(context).pop(),
              ),
              FlatButton(
                child: Text("Confirm"),
                onPressed: () => Navigator.of(context).pop(_withTree),
              )
            ],
          );
        },
      );
    }



    return Scaffold(
      appBar: AppBar(
        title: Text("Checking Dialog"),
      ),
      body: Padding(
        padding: EdgeInsets.only(
          top: 18,
          bottom: 18
        ),
        child: ListView(
          children: <Widget>[
            AlertItem("Checking Dialog", () async {
              bool deleteTree = await showDeleteConfirmDialog();
              if (deleteTree == null) {
                alert(context, content: "Cancel");
              } else {
                alert(context, content: "Delete Sub Folders : $_withTree - $_withTree2 - $_withTree3 - $_withTree4");
              }
            }),
          ],
        ),
      ),
    );
  }
}




class _DialogCheckBox extends StatefulWidget {

  final bool value;
  final ValueChanged<bool> onChanged;
  _DialogCheckBox({
    Key key,
    this.value,
    @required this.onChanged
  });

  @override
  _DialogCheckBoxState createState() => _DialogCheckBoxState();
}
class _DialogCheckBoxState extends State<_DialogCheckBox> {

  bool value;

  @override
  void initState() {
    super.initState();
    value = widget.value;
  }
  @override
  Widget build(BuildContext context) {
    return Checkbox(
      value: value,
      onChanged: (v) {
        widget.onChanged(v);
        setState(() {
          value = v;
        });
      },
    );
  }
}